package 双指针;

public class 有序数组的平方 {
    public int[] sortedSquares(int[] nums) {
        int n = nums.length;
        int i=0,j=n-1,k=n-1;
        int[] ans = new int[n];
        for (int l = 0; l < n; l++) {
            if (nums[i]*nums[i]<nums[j]*nums[j]){
                ans[k--] = nums[j]*nums[j];
                j--;
            }
            else{
                ans[k--] = nums[i]*nums[i];
                i++;
            }
        }
        return ans;
    }
}
